package com.hlhj.funlike_business.activity.fragment;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.mymvp.mvp.BaseFgm;
import com.flyco.tablayout.SlidingTabLayout;
import com.hlhj.funlike_business.CustView.LazyViewPager;
import com.hlhj.funlike_business.CustView.MyViewPager;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.AddCommentyAty;
import com.hlhj.funlike_business.activity.adapter.FoodTitleAdp;
import com.hlhj.funlike_business.activity.adapter.TabFrgAdapter;
import com.hlhj.funlike_business.bean.FoodTypeBean;
import com.hlhj.funlike_business.presenter.MineFoodFgmPresenter;
import com.hlhj.funlike_business.views.MineFoodFgmView;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class MineFoodFragment1 extends BaseFgm<MineFoodFgmView, MineFoodFgmPresenter> implements MineFoodFgmView {
    private TextView tv_addfood;
    private LazyViewPager mAnchorViewPager;
    private TabLayout mAnchorTabLayout;
    // adapter
    private FoodTitleAdp mViewPagerAdapter;
    private List<String> stringList;
    private List<Fragment> fragments;
    private FoodFragment foodFragment;

    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_minefood1;
    }

    @NotNull
    @Override
    public MineFoodFgmView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MineFoodFgmPresenter createPresenter() {
        return new MineFoodFgmPresenter();
    }

    @Override
    public void initView() {
        mAnchorViewPager = rootView.findViewById(R.id.viewpager);
        mAnchorTabLayout = rootView.findViewById(R.id.tabLayout);
        tv_addfood = rootView.findViewById(R.id.tv_addfood);

    }

    @Override
    public void logicStart() {
        stringList = new ArrayList<>();
        fragments = new ArrayList<>();
        stringList.add("全部商品");     //默认为0
        fragments.clear();
        for (int i = 0; i < stringList.size(); i++) {
            foodFragment = FoodFragment.newInstance(0);
            fragments.add(foodFragment);
        }
        mViewPagerAdapter = new FoodTitleAdp(getChildFragmentManager(), stringList, fragments);

        getPresenter().LoadFoodList(getContext());
        mAnchorViewPager.setOnPageChangeListener(mViewPagerChangeListener);
    }

    @Override
    public void setListener() {
        tv_addfood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddCommentyAty.class);
                intent.putExtra("id",0);
                intent.putExtra("type",0);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void showFoodList(List<FoodTypeBean.DataBean> dataBeans) {
        if (dataBeans!=null){
            stringList.clear();
        }
        dataBeans.add(0,new FoodTypeBean.DataBean(0,"全部商品"));
        for (FoodTypeBean.DataBean foodTypeBean : dataBeans) {
            stringList.add(foodTypeBean.getType_name());
            mAnchorTabLayout.addTab(mAnchorTabLayout.newTab().setText(foodTypeBean.getType_name()));
        }

        fragments.clear();
        for (int i = 0; i < dataBeans.size(); i++) {
            foodFragment = FoodFragment.newInstance(dataBeans.get(i).getId());
            fragments.add(foodFragment);
        }
        mViewPagerAdapter = new FoodTitleAdp(getChildFragmentManager(), stringList, fragments);
        initViewPagerAdapter(fragments,stringList);
        mViewPagerAdapter.notifyDataSetChanged();

    }

    private void initViewPagerAdapter(List<Fragment> fragments, List<String> tabTitles) {

        // set view pager adapter
        if (mAnchorViewPager != null) {
            mAnchorViewPager.setAdapter(mViewPagerAdapter);
            // connect tablayout and viewpager
            //mAnchorTabLayout.setupWithViewPager(mAnchorViewPager);
        }

        // make click tab view item to make view pager to scroll to relevant position
        Log.e("zy",tabTitles.size() + "");
        for (int i = 0; i < mAnchorTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mAnchorTabLayout.getTabAt(i);
            if (tab != null) {
                // use reflect to get tab object, then to get Class
                Class c = tab.getClass();
                try {
                    // c.getDeclaredField get private attribute
                    // view is tab's private attribute, type is TabView, TabLayout private inner class
                    // if dependence com.android.support:design:28.0.0 use "view", if below this version, then the field name is mView
                    Field field = c.getDeclaredField("mView");
                    if (field == null) {
                        continue;
                    }
                    field.setAccessible(true);
                    final View view = (View) field.get(tab);
                    if (view == null) {
                        continue;
                    }
                    view.setTag(i);
                    view.setOnClickListener(mTabOnClickListener);
                } catch (NoSuchFieldException e) {
                    Log.e("zy", "NoSuchFieldException, message="+e.getMessage());
                } catch (IllegalAccessException e) {
                    Log.e("zy", "IllegalAccessException, message="+e.getMessage());
                } catch (Exception e) {
                    Log.e("zy", "Exception, message="+e.getMessage());
                }
            }
        }
    }

    // make tab layout scroll to correct position tab when viewpager scroll
    LazyViewPager.OnPageChangeListener mViewPagerChangeListener = new LazyViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mAnchorTabLayout.getTabAt(position).select();
        }

        @Override
        public void onPageSelected(int position) {
            mAnchorTabLayout.getTabAt(position).select();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            /* this function called when state is changed, state has 3 state:0, 1, 2
                state ==1 : page is scrolling
                state==2 : page is scroll finish
                state==0 : page do nothing */
        }
    };

    // make viewpager to relevant position when user click tablayout tab
    private View.OnClickListener mTabOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int pos = (int) view.getTag();
            TabLayout.Tab tab = mAnchorTabLayout.getTabAt(pos);
            if (tab != null) {
                tab.select();
            }
            mAnchorViewPager.setCurrentItem(pos, false);
        }
    };
}
